<?php
defined('SYS_RUN') or exit();

class Database {

    private $_connections = array();

    function __construct() {
        $settings = Settings::get('db');
        if($settings) {
            foreach($settings as $dbName => $dbSettings) {
                switch ($dbName) {
                    case 'mongo':
                    case 'storage':
                        $connectionString = 'mongodb://';
                        if($dbSettings['user'])
                            $connectionString .= $dbSettings['user'].':'.$dbSettings['password'].'@';
                        $connectionString .= $dbSettings['host'];
                        try {
                            $connection = new MongoClient($connectionString);
                            $this->_connections[$dbName] = array(
                                    'connection' => $connection,
                                    'dbConnection' => $this->_prepareConnection($dbName, $connection, $dbSettings['db'])
                                );
                        } catch (DbException $e) {
                            throw new DbException($e->getMessage());
                        }
                    break;
                }
            }
        }
    }

    function __destruct() {
        if(count($this->_connections) > 0) {
            foreach($this->_connections as $name => $connection) {
                switch ($name) {
                    case 'mongo':
                    case 'storage':
                        $connections = $connection['connection']->getConnections();
                        foreach ( $connections as $con ) {
                            // Loop over all the connections, and when the type is "SECONDARY"
                            // we close the connection
                            if ( $con['connection']['connection_type_desc'] == "SECONDARY" ) {
                                $connection->close( $con['hash'] );
                            }
                        }
                        break;
                }
            }
        }
    }

    public function getConnection($name) {
        if(isset($this->_connections[$name])) {
            return $this->_connections[$name]['dbConnection'];
        }
        return false;
    }

    /**
     * Get Mongo connection object
     *
     * @return bool | object
     */
    public function mongo() {
        return $this->getConnection('mongo');
    }

    /**
     * Get Mongo GridFS connection object
     *
     * @return bool | object
     */
    public function storage() {
        return $this->getConnection('storage');
    }

    private function _prepareConnection($database, $connection, $dbname) {
        switch ($database) {
            case 'mongo':
                return $connection->selectDB($dbname);
            case 'storage':
                return $connection->selectDB($dbname)->getGridFS();
            break;
        }
    }
}